home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-20 / rs0422.zip / LEVEL1 / LEVEL1.AS < prev    next >
Text File  |  1990-11-13  |  21KB  |  809 lines

  1. ;/*
  2. ; * Copyright 1988 by the Radio Amateur Telecommunications Society
  3. ; * and Thomas A. Moulton, W2VY
  4. ; *
  5. ; * This software may only be modified, copied, distributed or
  6. ; * executed for non-profit purposes by individuals operating
  7. ; * systems in the Amateur Radio Service.  Credit to the
  8. ; * author(s) and to the Radio Amateur Telecommunications Society
  9. ; * must be made in modules where RATS provided software is used,
  10. ; * and in any announcements and documentation.  
  11. ; *
  12. ; * As a non-profit, research and development organization,  the
  13. ; * Radio Amateur Telecommunications Society distributes software
  14. ; * in both executable and source forms.  This policy is in place
  15. ; * to encourage the development and distribution of OSI-based,
  16. ; * networking tools.  In order to protect the interests of the
  17. ; * Society and the authors, we have placed some conditions
  18. ; * of use on the software.  Other groups are encouraged
  19. ; * to place the same or similar guidelines on
  20. ; * software they produce.
  21. ; *
  22. ; * The Radio Amateur Telecommunications Society reserves the right
  23. ; * to specify and alter the terms under which software provided by
  24. ; * the Society may be used.  This policy is consistent with the 
  25. ; * objective of uniform and consistent "Open Systems Interconnections."
  26. ; * 
  27. ; * All acceptable Amateur Radio related uses of this software
  28. ; * will be outlined in the "ROSE Implementer's Guide".  Individuals
  29. ; * or organizations wishing to add to, or modify the provisions of
  30. ; * the guide to accommodate local or evolutionary requirements
  31. ; * should document the proposed change(s) and forward them to the
  32. ; * Society.  If accepted, written notification will be provided by
  33. ; * the Society to the submitting organization or individual(s).
  34. ; * The Society will then issue a "ROSE Implementer's Guide Change
  35. ; * Notice".  Periodically, the Society will re-issue the "ROSE 
  36. ; * Implementer's Guide" and incorporate the text of the change 
  37. ; * notices.  This procedure has been put in to place to ensure
  38. ; * compatibility between systems and to ensure their "Openness"
  39. ; * and interoperability.
  40. ; *
  41. ; * No part of this software may be used in other packages 
  42. ; * without prior authorization from the author or the Society.  
  43. ; * Software incorporating this module, all or in part, must be 
  44. ; * provided to the Society prior to distribution or use by
  45. ; * anyone not directly involved in testing of the revised  
  46. ; * environment.  Current releases of the combined software must
  47. ; * be provided to the Society in both source and executable
  48. ; * forms.  Adequate documention to produce an executable module 
  49. ; * from the provided source must also be included.
  50. ; *
  51. ; * Non-Amateur Radio non-profit uses may be authorized on a case
  52. ; * by case basis.  Inquiries for such use may be made in writing
  53. ; * to the Society. Non-commercial uses consistent with the
  54. ; * general principles of Open Systems Interconnection Reference
  55. ; * Model will be generally considered with favor.
  56. ; *
  57. ; * Commercial licensing of the software is also available based
  58. ; * on normal commercial terms.  Licensing inquiries should be
  59. ; * directed to the Society.  Commercial licensing of the standard
  60. ; * software will be done in situations which materially benefit
  61. ; * the Amateur Radio Packet Network.  Additional licensing is
  62. ; * reserved by the individual authors.
  63. ; * 
  64. ; * The Radio Amateur Telecommunications Society provides this software
  65. ; * on an "as is" basis.  The Society assumes no liability for
  66. ; * loss incurred through the use of this software.  Amateur Radio
  67. ; * use of this software implies non-commercial and voluntary 
  68. ; * development, deployment and use of this software in a "Amateur",
  69. ; * non-commercial service.  Commercial users are encouraged to
  70. ; * inspect their copies of the source code.  Source code modification
  71. ; * licenses are available if a combined Object and Source Code
  72. ; * license was not originally established.
  73. ; * 
  74. ; * The Society may be contacted by writing or calling at:
  75. ; * 
  76. ; * The Radio Amateur Telecommunications Society 
  77. ; * 206 North Vivyen Street.
  78. ; * Bergenfield, New Jersey 07621
  79. ; *
  80. ; * Telephone: 201-387-8896
  81. ; *
  82. ; */
  83. *Title Level 2 Interface routines to the SIO
  84.  
  85. ;
  86. *INCLIDE RCONFIG.LIB
  87. *INCLUDE STRUCT.LIB
  88.  
  89. ;    Option for OS_QUE to place task on the top instead of the end
  90. FORCE    EQU    80h    ;Low bits are # of arg in DE, BC and HL=A(rtn)
  91.  
  92. ;    Structure of a Packet (FIX if BUFFER.H is changed)
  93. ;
  94. NEXTS    EQU    0    ;Pointer to next segment
  95. NEXT    EQU    2    ;Pointer to next Packet
  96. RDATA    EQU    4    ;Pointer to next char to be read from buffer
  97. WDATA    EQU    6    ;Pointer to next char written to buffer
  98. BUFMAX    EQU    8    ;Pointer to last ch in segment
  99. BUFSIZE EQU    10    ;Size of this structure
  100.  
  101.     PSECT    bss
  102.     GLOBAL    _COLD
  103.     GLOBAL    QTRASH, QUEFUL, QUEUE, QSLOTS, TWEEK, _L1BSSL, _L1BSS
  104. _L1BSS    EQU    $
  105. QTRASH: DEFS    1
  106. QUEFUL:    DEFS    1
  107. QUEUE:    DEFS    4    ;STRUCT QUEUE
  108. QMAX    EQU    0    ;MAX    DS    1 WITH LD HL,(QUEUE) THIS IS L
  109. QUSED    EQU    1    ;USED    DS    1                 H
  110. QBOT    EQU    2    ;BOT    DS    1 WITH LD HL,(QUEUE+BOT)     L
  111. QTOP    EQU    3    ;TOP    DS    1                 H
  112. QSLOTS:    DEFS    NSLOT*4
  113. TWEEK:    defs    1
  114. _L1BSSL    EQU    $-QTRASH
  115.  
  116. IXSAVE:    DEFS    2
  117. IYSAVE:    DEFS    2
  118. SPSAVE:    DEFS    2
  119. TRTN:    DEFS    2
  120. ;    FREEQ:    DEFS    2
  121. ;    _INTS_OFF: DEFS 1
  122.  
  123.     PSECT    text
  124.     GLOBAL    csv, rcsv, cret, indir
  125.     GLOBAL    _free_pktx, _L1_RECV, GETSIO, XSKED
  126.     GLOBAL    _bgetch, _bappch, _new_buffer
  127.  
  128. freeit:    PUSH    DE        ;free_pkt(DE) == free_pktx(&DE)
  129.     LD    HL,0        ;Push the address of the param on stack
  130.     ADD    HL,SP
  131.     PUSH    HL
  132.     CALL    _free_pktx    ;BAD CHAN #, FREE THE PACKET
  133.     POP    DE
  134.     POP    DE
  135.     LD    HL,0
  136. ;            Fall Through...
  137. EXIT:    POP    IX
  138.     EI
  139.     JP    cret
  140.  
  141.     GLOBAL    _SIO_SND
  142. _SIO_SND:
  143.     CALL    rcsv    ;SIO_SND(port,pkt) HL=port DE=pkt
  144.     PUSH    IX    ;SAVE THE C REGS
  145.     LD    A,E    ;IS THE PACKET NULL?
  146.     OR    D
  147.     JP    Z,EXIT
  148.  
  149.     CALL    GETSIO    ;CONVERT HL FROM PORT # TO SDS STRUCT, TRASHES BC
  150.     JP    Z,freeit
  151.     PUSH    HL
  152.     POP    IY    ;GET POINTER TO MY STRUCT
  153.     DI
  154.     LD    L,(IY+TXQ)
  155.     LD    H,(IY+(TXQ+1))
  156.     LD    A,L        ;IS THE TXQ EMPTY?
  157.     OR    H
  158.     JP    NZ,ATEND    ;NOPE, ADD TO END
  159.     LD    (IY+TXQ),E    ;Put new item on top of queue
  160.     LD    (IY+(TXQ+1)),D
  161.     JP    SENDING
  162.  
  163. ATEND:    PUSH    HL
  164.     POP    IX        ;NEED TO SAVE IY FOR INTR RTNS
  165.     LD    L,(IX+NEXT)    ;TRY NEXT ENTRY
  166.     LD    H,(IX+(NEXT+1))
  167.     LD    A,L
  168.     OR    H        ;IS THE NEXT NULL?
  169.     JP    NZ,ATEND    ;NOPE, KEEP SCANNING
  170.     LD    (IX+NEXT),E
  171.     LD    (IX+(NEXT+1)),D    ;STUFF IT INTO THE LAST PKT
  172. SENDING:
  173.     PUSH    DE
  174.     POP    IX
  175.     LD    (IX+NEXT),0    ;INSURE IT POINTS TO NULL
  176.     LD    (IX+(NEXT+1)),0
  177.  
  178.     LD    A,(IY+TSTA)    ;ARE WE SENDING?
  179.     OR    A
  180.     CALL    Z,XSKED        ;WE ARE IDLE, START US UP AGAIN
  181.     JP    EXIT
  182. ;
  183.  
  184.     GLOBAL    SIOTXCH
  185. SIOTXCH:
  186.     LD    C,(IY+TXSEG)    ;GET POINTER TO CURRENT SEG
  187.     LD    B,(IY+(TXSEG+1))
  188.     LD    A,B
  189.     OR    C        ;IS IT NULL?
  190.     JP    NZ,TXCHAR    ;NOPE, CHECK FOR DATA
  191.     BIT    AFT,(IY+FLAG)    ;Using AFT?
  192.     JP    Z,1f
  193.     LD    A,(IY+TXAFT)    ;What is our state?
  194.     SUB    3
  195.     JP    M,1f        ;Less than 3
  196.     INC    A
  197.     JP    AFTCRC        ;Sending CRC and ending flag
  198.  
  199. 1:    LD    C,(IY+TXQ)    ;ANYTHING WAITING?
  200.     LD    B,(IY+(TXQ+1))
  201.     LD    A,B
  202.     OR    C
  203.     LD    A,1        ;ASSUME NOTHING WAITING
  204.     RET    Z        ;Everything NULL, nothing to do
  205.     LD    (IY+TXSEG),C    ;STUFF IT AND GET THIS SHOW ON THE ROAD
  206.     LD    (IY+(TXSEG+1)),B
  207.     PUSH    BC
  208.     POP    IX        ;TXSEG=TXQ
  209.     LD    A,(IX+NEXT)
  210.     LD    (IY+TXQ),A    ;TXQ=TXSEG->NEXT
  211.     LD    A,(IX+(NEXT+1))
  212.     LD    (IY+(TXQ+1)),A
  213.     LD    A,0
  214.     LD    (IX+NEXT),A
  215.     LD    (IX+(NEXT+1)),A    ;TXSEG->NEXT=NULL
  216.  
  217. TXCHAR:    BIT    AFT,(IY+FLAG)    ;Using AFT?
  218.     JP    Z,1f
  219.     LD    A,(IY+TXAFT)    ;What is my state?
  220.     OR    A        ;Idle? (State 0)
  221.     JP    Z,TXFLAG    ;Yes, Send Flag instead of data
  222.     DEC    A        ;Get next byte? (State 1)
  223.     JP    NZ,AFTBYT    ;No, do AFT Handling
  224. 1:    PUSH    IY        ;Get the next byte from buffer
  225.     POP    HL
  226.     LD    BC,TXSEG    ;IY+TXSEG IS THE POINTER TO THE CURRENT
  227.     ADD    HL,BC        ;PACKET BEGIN SENT
  228.     PUSH    HL
  229.     CALL    _bgetch        ;bgetch(&TXSEG)
  230.     POP    BC        ;RETURNS CHAR OR -1 IF EOF
  231.     LD    A,H
  232.     OR    A        ;IS IT A CHAR?
  233.     JP    NZ,TXEOF
  234.     LD    B,L
  235.     DEC    A        ;It's a Char (A now = 0FFh)
  236.     BIT    AFT,(IY+FLAG)    ;AFT?
  237.     RET    Z        ;Nope, done
  238.     LD    L,(IY+TXCRC)
  239.     LD    H,(IY+(TXCRC+1))
  240.     CALL    CRCA        ;Compute AFT CRC, Char in B, CRC in HL
  241.     LD    (IY+TXCRC),L
  242.     LD    (IY+(TXCRC+1)),H
  243.     LD    A,7Dh
  244.     CP    B        ;Is it the AFT Escape Char?
  245.     JP    Z,1f
  246.     INC    A
  247.     CP    B        ;Is it the AFT Flag?
  248.     LD    A,0FFh        ;Assume it's just a data byte
  249.     RET    NZ
  250. 1:    LD    A,B        ;Restore char
  251.     XOR    20h        ;Do Transparency
  252.     LD    (IY+TXAFT),2    ;Need to send TBUF next
  253.     LD    (IY+TBUF),A    ;Save it
  254.     LD    B,7Dh        ;Send Escape
  255.     LD    A,0FFh        ;It's data
  256.     RET
  257.  
  258. TXFLAG:    LD    B,7Eh        ;AFT Start Flag
  259.     LD    (IY+TXAFT),1    ;Now in data transfer
  260.     LD    A,0FFh        ;Treat it as data
  261.     LD    (IY+TXCRC),A
  262.     LD    (IY+(TXCRC+1)),A ;Reset TX CRC
  263.     RET
  264.  
  265. TXEOF:    BIT    AFT,(IY+FLAG)    ;AFT?
  266.     JP    NZ,AFTCR    ;Yes, push out the CRC
  267. TXMOR:    LD    A,(IY+TXQ)
  268.     OR    (IY+(TXQ+1))    ;MORE WAITING?
  269.     LD    A,0
  270.     RET    NZ        ;YES
  271.     LD    A,1
  272.     RET            ;NOPE, EOT
  273.  
  274. AFTBYT:    DEC    A        ;In transparency sequence? (State 2)
  275.     JP    NZ,AFTCRC    ;Sending CRC
  276.     LD    B,(IY+TBUF)
  277.     LD    A,0FFh        ;Send as Data
  278.     LD    (IY+TXAFT),1    ;Back in normal Data Transfer
  279.     RET
  280.  
  281. AFTCR:    LD    (IY+TXAFT),4    ;Need to send high byte of CRC next
  282.     LD    B,(IY+TXCRC)    ;Get low byte to send now
  283.     JP    AFTCRT
  284.  
  285. AFTCRC:    DEC    A        ;State 3 (Send TBUF for Low Byte)
  286.     JP    Z,AFTCRB    ;Yes, Send TBUF
  287.     DEC    A        ;State 4 (Send High Byte)
  288.     JP    NZ,AFTCR8    ;Nope...
  289.     LD    (IY+TXAFT),6    ;Ending Flag Next
  290.     LD    B,(IY+(TXCRC+1)) ;High byte needs to be sent (State 4)
  291.     JP    AFTCRT
  292.  
  293. AFTCR8:    DEC    A        ;State 5 (Send TBUF for High Byte)
  294.     JP    NZ,TXFLAG    ;Send Ending Flag
  295. AFTCRB:    LD    B,(IY+TBUF)
  296.     INC    (IY+TXAFT)    ;Advance state
  297.     LD    A,0FFh        ;Data to send
  298.     RET
  299.  
  300. AFTCRT:    LD    A,B        ;Handle and Translate a CRC byte
  301.     XOR    0FFh        ;1's comp
  302.     LD    B,A
  303.     CP    7Dh        ;AFT Escape?
  304.     JP    Z,1f
  305.     CP    7Eh        ;AFT Flag?
  306.     JP    Z,1f
  307.     LD    A,0FFh        ;Data
  308.     RET
  309.  
  310. 1:    XOR    20H
  311.     LD    (IY+TBUF),A    ;Save Translated Char
  312.     LD    B,7Dh        ;Escape
  313.     LD    A,0FFh        ;Data to Send
  314.     DEC    (IY+TXAFT)    ;Back up to translate state
  315.     RET
  316.  
  317.     GLOBAL    TXODONE
  318. TXODONE:
  319.     LD    E,(IY+TXQ)    ;IS THERE STILL MORE TO TX?
  320.     LD    D,(IY+(TXQ+1))
  321.     LD    A,E
  322.     OR    D
  323.     JP    NZ,XSKED    ;Start sending again!
  324.     LD    (IY+TXAFT),0    ;Idle State force starting
  325.     RET            ;Flag for next AFT frame
  326.  
  327.     GLOBAL    SIORXCH
  328. SIORXCH:
  329.     LD    E,(IY+RXQ)    ;GET THE CURRENT PKT
  330.     LD    D,(IY+(RXQ+1))
  331.     LD    C,A        ;SAVE A
  332.     LD    A,E
  333.     OR    D
  334.     JP    Z,RXABORT        ;NO PACKET TO FILL, ABORT
  335.     LD    A,C    
  336.     PUSH    DE
  337.     POP    IX        ;Put in IX too
  338.  
  339.     CP    0FFh        ;RX DATA?
  340.     JP    Z,RXCHAR    ;Yes, STUFF IT!
  341.     CP    0        ;START OF FRAME?
  342.     JP    NZ,RXEND    ;NOPE, MAYBE END
  343.     LD    A,(IX+RDATA)    ;GET THE INITIAL WRITE POINTER
  344.     LD    (IX+WDATA),A    ;RESET IT TO BEGINING
  345.     LD    A,(IX+(RDATA+1))
  346.     LD    (IX+(WDATA+1)),A ;PKT->WDATA=PKT->RDATA
  347. RXCHAR:
  348.     BIT    AFT,(IY+FLAG)    ;Using AFT?
  349.     JP    NZ,AFTRX
  350. RXCH:    LD    H,0
  351.     LD    L,B        ;GET THE RX CHAR
  352.     PUSH    HL
  353.     PUSH    DE        ;PACKET TO STUFF IT INTO
  354.     CALL    _bappch        ;bappch(RXQ,CH)
  355.     POP    BC        ;RETURNS -1 (IN HL) IF NO SPACE
  356.     POP    BC
  357.     LD    A,L
  358.     OR    H        ;True if buffer is now full
  359.     BIT    ASYNC,(IY+FLAG)    ;Simple Async Stream doesn't care
  360.     JP    NZ,RXASY
  361.     OR    A
  362.     RET    Z
  363.     JP    RXABORT        ;Must be HDLC, is an error
  364.  
  365. AFTRX:    LD    A,(IY+RXAFT)    ;Get State
  366.     OR    A        ;Looking for a Flag? (State 0)
  367.     JP    Z,AFTFLG    ;Yes
  368.     DEC    A        ;Dxfer? (State 1)
  369.     JP    NZ,AFTTR    ;Must be in Escape Sequence (State 2)
  370.     LD    A,7Dh        ;Escape?
  371.     CP    B
  372.     JP    NZ,1f        ;Nope
  373.     LD    (IY+RXAFT),2    ;Next char needs to be translated
  374.     LD    A,0
  375.     RET
  376. 1:    INC    A        ;Flag?
  377.     CP    B
  378.     JP    NZ,CRAFT    ;Compute CRC and return Char
  379.     LD    A,(IY+RXCRCL)    ;Rx anything since last flag?
  380.     OR    A
  381.     RET    Z        ;Nope, was Flag-Flag-...
  382.     LD    (IY+RXAFT),1    ;Got Ending Flag, treat as opening flag too
  383.     JP    RXAEOF
  384.  
  385. AFTTR:    DEC    A
  386.     JP    NZ,RXABORT    ;Illegal State (State greater than 2)
  387.     LD    A,7Eh        ;Insure it's not a flag (Abort sequence)
  388.     CP    B
  389.     JP    Z,RXABORT    ;Esc-Flag, Abort sequence
  390.     LD    A,20h
  391.     XOR    B
  392.     LD    B,A        ;Translated Char
  393.     LD    (IY+RXAFT),1    ;Back to normal
  394. CRAFT:    LD    L,(IY+RXCRC)
  395.     LD    H,(IY+(RXCRC+1))
  396.     CALL    CRCA        ;Compute AFT CRC, Char in B, CRC in HL
  397.     LD    (IY+RXCRC),L
  398.     LD    (IY+(RXCRC+1)),H
  399.     LD    A,(IY+RXCRCL)    ;Is the crc buffer full?
  400.     CP    2
  401.     JP    M,CRFIL         ;Nope, Fill it up!
  402.     CP    3         ;Have we had any data yet?
  403.     JP    Z,1f
  404.     INC    (IY+RXCRCL)     ;Remember that there was data in the frame
  405. 1:    LD    C,(IY+CRBUF)     ;Get Top
  406.     LD    A,(IY+(CRBUF+1)) ;Move Second to
  407.     LD    (IY+CRBUF),A     ;Top
  408.     LD    (IY+(CRBUF+1)),B ;Put New in Second
  409.     LD    B,C         ;Stuff Old top into our buffer
  410.     JP    RXCH
  411.  
  412. CRFIL:    INC    (IY+RXCRCL)    ;Adding a byte to the buffer
  413.     OR    A        ;Do we have any bytes?
  414.     JP    Z,1f        ;Nope, make it the top item
  415.     DEC    A        ;Do we have 1 byte?
  416.     JP    NZ,RXABORT    ;Nope, we are Messed up!
  417.     LD    (IY+(CRBUF+1)),B ;Make it the Second Item
  418.     RET
  419.  
  420. 1:    LD    (IY+CRBUF),B    ;Top Item
  421.     RET
  422.  
  423. AFTFLG:    LD    A,7Eh        ;AFT Flag Char
  424.     CP    B
  425.     LD    A,0
  426.       RET    NZ        ;Not Flag, ignore
  427.     DEC    A
  428.     LD    (IY+RXCRC),A    ;Opening Flag, Reset CRC
  429.     LD    (IY+(RXCRC+1)),A
  430.     INC    A        ;Return No Error (0)
  431.     LD    (IY+RXAFT),1    ;In Data Transfer
  432.     RET
  433.  
  434. RXASY:    OR    A        ;EOF?
  435.     JP    Z,RXASY1    ;No...xfer
  436.     BIT    AFT,(IY+FLAG)    ;Buffer full error - get Flags
  437.     JP    Z,RXAEOF    ;Yes, Buffer is full, Not AFT - return buffer
  438.     JP    RXABORT        ;This is an error for AFT - discard
  439. RXASY1:    BIT    AFT,(IY+FLAG)    ;AFT Doesn't use CR for forwarding of data
  440.     JP    NZ,1f
  441.     LD    A,C        ;Get char back
  442.     AND    7FH        ;STRIP PARITY
  443.     CP    0DH        ;IS IT A CR?
  444. 1:    LD    A,0        ;Assume No
  445.     RET    NZ        ;NO, MORE TO FOLLOW!
  446. RXAEOF:    LD    (IY+RSTA),0    ;Async back in idle state
  447.     LD    A,1        ;End of Frame (Async).
  448. RXEND:    CP    1        ;END OF FRAME?
  449.     JP    NZ,RXABRT    ;MAYBE A FRAME ABORT?
  450.     BIT    AFT,(IY+FLAG)    ;Using AFT?
  451.     JP    Z,1f
  452.     LD    A,0B8h        ;Correct CRC?
  453.     CP    (IY+RXCRC)
  454.     JP    NZ,RXABORT    ;Bad, Abort
  455.     LD    A,0F0h        ;Correct CRC?
  456.     CP    (IY+(RXCRC+1))
  457.     JP    NZ,RXABORT    ;Abort
  458.     LD    A,3        ;Did we get atleast 3 Bytes?
  459.     CP    (IY+RXCRCL)
  460.     JP    NZ,RXABORT
  461.     XOR    A
  462.     LD    (IY+RXCRCL),A    ;Reset some stuff for the next frame
  463.     DEC    A
  464.     LD    (IY+RXCRC),A
  465.     LD    (IY+(RXCRC+1)),A
  466. 1:    LD    C,(IX+NEXT)    ;RXQ=RXQ->NEXT
  467.     LD    B,(IX+(NEXT+1))
  468.     LD    (IY+RXQ),C
  469.     LD    (IY+(RXQ+1)),B
  470.     LD    (IX+NEXT),0    ;PKT->NEXT=NULL
  471.     LD    (IX+(NEXT+1)),0
  472.     PUSH    IX        ;Restore DE
  473.     POP    DE
  474. QUERX:    LD    B,0
  475.     LD    C,(IY+CHAN)    ;GET THE CHANNEL NUMBER
  476.     LD    HL,_L1_RECV
  477.     LD    A,2
  478.     CALL    OS_QUE
  479.     LD    A,0
  480.     RET
  481.  
  482. RXABORT:
  483.     XOR    A
  484.     LD    (IY+RXAFT),A    ;AFT Flag Hunt
  485.     LD    (IY+RXCRCL),A    ;No data yet
  486.     LD    A,0FFh        ;FAILED, ENTER FLAG HUNT
  487.     LD    (IY+RXCRC),A
  488.     LD    (IY+(RXCRC+1)),A ;Reset AFT RX CRC
  489.     RET
  490.  
  491. RXABRT:    CP    2        ;FRAME ABORTED?
  492.     LD    A,0FFh        ;NOTE THE ERROR
  493.     RET    NZ        ;INVALID, IGNORE CALL
  494.     LD    A,(IX+RDATA)    ;RESET THE CURRENT WRITE POINTER
  495.     LD    (IX+WDATA),A
  496.     LD    A,(IX+(RDATA+1))
  497.     LD    (IX+(WDATA+1)),A
  498.     LD    A,0
  499.     RET
  500.  
  501. CRCA:    ;Compute AFT CRC, Char in B, CRC in HL, DON'T Use DE
  502.     PUSH    BC    ;Save Char
  503.     LD    C,B    ;Need B for the loop
  504.     LD    B,8    ;8 bits per octet
  505. CRCB:    LD    A,C    ;Get current bit
  506.     XOR    L
  507.     SRL    H    ;fcs = fcs>>1
  508.     RR    L
  509.     RR    A
  510.     JP    NC,1f
  511.     LD    A,84h
  512.     XOR    H
  513.     LD    H,A    ;fcs = fcs ^ 0x8408
  514.     LD    A,08h
  515.     XOR    L
  516.     LD    L,A
  517. 1:    RR    C    ;Advance to next bit
  518.     DJNZ    CRCB
  519.     POP    BC    ;Restore Char
  520.     RET
  521.  
  522.     global    _RX_GIVE
  523. _RX_GIVE:            ;RX_GIVE(CHAN,PKT,depth)
  524.     CALL    rcsv        ;      HL  DE
  525.     PUSH    IX    ;SAVE THE C REGS
  526.  
  527.     LD    A,E    ;IS THE PACKET NULL?
  528.     OR    D
  529.     JP    Z,RXGNIL    ;DON'T HAVE A PACKET
  530.     CALL    GETSIO        ;FIND THE SDS STRUCT
  531.     JP    Z,freeit    ;BAD CHAN NUMBER FREE THE PACKET
  532.     PUSH    HL
  533.     POP    IY        ;GET THE SDS POINTER
  534.     DI
  535.     LD    B,(IX+10)    ;MAX # OF PACKETS ON A RECV QUEUE (par 3)
  536.     LD    L,(IY+RXQ)    ;GET THE RX POOL POINTER
  537.     LD    H,(IY+(RXQ+1))
  538.     LD    A,L
  539.     OR    H
  540.     JP    NZ,APPEND    ;APPEND IT TO THE QUEUE
  541.     LD    (IY+RXQ),E    ;STUFF IT, HOW MUCH DID WE MISS?
  542.     LD    (IY+(RXQ+1)),D
  543.     LD    HL,1        ;We Want more packets
  544.     JP    1f
  545. RXGNIL:    LD    HL,0        ;RETURN FALSE
  546. 1:    JP    EXIT        ;START RX'ING AGAIN!
  547.  
  548. APPEND:
  549.     PUSH    HL
  550.     POP    IX        ;GET THE TOP PACKET
  551.     LD    L,(IX+NEXT)    ;HL=IX->NEXT
  552.     LD    H,(IX+(NEXT+1))
  553.     LD    A,L
  554.     OR    H
  555.     JP    Z,APEND        ;FOUND THE END
  556. ;    EI        ;Allow Ints - We have the thing the top points to
  557.     DJNZ    APPEND        ;LOOP UPTO 10 TIMES
  558.     EI            ;I GIVE UP, TOO MUCH HERE FOR HIM
  559.     JP    freeit        ;freeit returns 0 (FALSE)
  560.  
  561. APEND:    LD    (IX+NEXT),E    ;IX->NEXT=DE
  562.     LD    (IX+(NEXT+1)),D
  563.     LD    HL,1        ;We Want more packets return TRUE
  564.     JP    EXIT
  565.  
  566.     GLOBAL    _OS_QUE
  567. _OS_QUE:    ;OS_QUE(rnt,P1)    CONVERT TO ASM CONVENTIONS
  568.     DI
  569.     CALL    rcsv
  570.     PUSH    IX    ;SAVE THE C REG
  571.     LD    A,1
  572.     CALL    OS_QUE
  573.     POP    IX    ;RESTORE C REG
  574.     EI        ; Ints were enabled, re-enable
  575.     JP    cret
  576.  
  577.  
  578.     GLOBAL    OS_QUE, OS_TASK
  579. OS_QUE:            ;SCHEDULE SOMETHING TO HAPPEN    ints OFF or die
  580.             ;HL IS THE A(ROUTINE)
  581.             ;A = 8x (HEX) TO FORCE THIS ITEM TO BE ON TOP
  582.             ;A   xN    WHERE N IS THE NUMBER OF ARGS
  583.             ;DE IS ARG #1 IF A=1 OR A=2
  584.             ;BC IS ARG #2 IF A=2
  585.     PUSH    BC    ;SAVE ARG #2
  586.     PUSH    DE    ;SAVE ARG #1
  587.     PUSH    HL    ;SAVE ADDRESS
  588.  
  589.     LD    HL,(QUEUE)    ;H=# USED. L=MAX
  590.     CP    3
  591.     JP    P,BADARG    ;TOO MANY ARGS REQUESTED!
  592.     AND    07h        ;CLEAR HIGH BITS
  593.     PUSH    AF    ;SAVE LEN
  594.     LD    B,1        ;SLOTS NEEDED FOR #ARG =0
  595.     CP    0        ;IS THAT WHAT WAS ASKED FOR?
  596.     JP    Z,1f        ;YUP
  597.     LD    B,2        ;FOR #ARGS = 1 OR 2 WE NEED 2 SLOTS
  598. 1:    LD    A,L
  599.     SUB    H        ;A=L-H (MAX-USED)
  600.     JP    Z,QUEFULL    ;OOPS, L=H
  601.     JP    M,QUEFULL    ;OOPS, L<H
  602.     CP    B        ;IF # SLOTS LEFT >= # SLOTS NEEDED WE ARE OK
  603.     JP    M,QUEFULL    ;HAVE ROOM
  604.     POP    AF
  605. ;    OR    A        ;IS FORCE (SIGN) BIT SET?
  606. ;    JP    M,QUEFRC    ;FORCE THIS ON THE TOP OF THE QUEUE
  607.     LD    C,A        ;FORM THE 1ST SLOT
  608.     LD    B,0        ;NUMBER OF ARGS
  609.     POP    DE        ;A(ROUTINE)
  610.     LD    A,(QUEUE+QBOT)    ;ADD TO THE BOTTOM
  611.     CALL    STUFF_IT    ;STUFF BC/DE IN SLOT # A
  612.     INC    A        ;BUMP IT UP, STUFF_IT WILL WRAP IT WHEN NEEDED
  613.     LD    (QUEUE+QBOT),A    ;SAVE POINTER
  614.     LD    A,C        ;# ARG
  615.     POP    BC
  616.     POP    DE        ;POP ARG 1 AND 2 IF PRESENT
  617.     CP    0        ;NO MORE ARGS?
  618.     RET    Z        ;ALL DONE
  619.     CP    2
  620.     JP    Z,2f        ; 2 ARGS
  621.     LD    DE,0        ;CLEAR ARG #2
  622. 2:    LD    A,(QUEUE+QBOT)    ;ADD TO BOTTOM
  623.     CALL    STUFF_IT
  624.     INC    A
  625.     LD    (QUEUE+QBOT),A    ;SAVE POINTER
  626.     RET
  627.  
  628.     COND    0        ;This code is not used at the present
  629. QUEFRC:    AND    7Fh        ;CLEAR FORCE BIT
  630.     JP    Z,FEASY        ;NO ARGS, EASY
  631.     LD    A,C
  632.     LD    B,0        ;# ARGS
  633.     POP    DE        ;A(ROUTINE)
  634.     LD    A,(QUEUE+QTOP)    ;ADD TO THE TOP
  635.     DEC    A
  636.     DEC    A        ;NEED TWO SLOTS
  637.     CALL    STUFF_IT    ;STUFF_IT WILL WRAP THE # IF NEEDED
  638.     LD    (QUEUE+QTOP),A    ;SAVE IT
  639.     LD    A,C        ;# OF ARG
  640.     POP    BC
  641.     POP    DE        ;ARG 1 AND 2
  642.     CP    2        ;2 ARGS?
  643.     JP    Z,1f        ;YES...
  644.     LD    DE,0        ;NOPE, CLEAR ARG 2
  645. 1:    LD    A,(QUEUE+QTOP)    ;WHERE TO PUT IT?
  646.     INC    A
  647.     CALL    STUFF_IT
  648.     RET
  649.  
  650. FEASY:    LD    C,A
  651.     LD    B,0
  652.     POP    DE        ;A(ROUTINE)
  653.     LD    A,(QUEUE+QTOP)    ;SLOT #
  654.     DEC    A
  655.     CALL    STUFF_IT
  656.     LD    (QUEUE+QTOP),A    ;Save pointer
  657.     POP    DE
  658.     POP    DE
  659.     RET
  660.     ENDC
  661.  
  662. QUEFULL:
  663.     LD    HL,QUEFUL    ;KEEP COUNT
  664.     INC    (HL)
  665.     JP    NZ,2f
  666.     DEC    (HL)        ;BUT DON'T WRAP
  667. 2:    POP    HL        ;Clean up stack - THANKS F6DWJ
  668. BADARG:
  669.     LD    HL,6        ;ADD 3 ARGS
  670.     ADD    HL,SP
  671.     LD    SP,HL
  672.     RET
  673.  
  674. STUFF_IT:
  675.     LD    HL,(QUEUE)    ;GET HEADER
  676.     INC    H        ;GONNA ADD ONE MORE
  677.     AND    L        ;QUEUE.MAX MUST BE (2^N)-1 EX FF, OR 7F, 3F, ...)
  678.     LD    (QUEUE),HL    ;SAVE HEADER, DONE WITH IT
  679.     LD    L,A
  680.     LD    H,0        ;MAKE DISPLACEMENT
  681.     ADD    HL,HL        ;SLOT # *2
  682.     ADD    HL,HL        ;SLOT # *4
  683.     PUSH    DE
  684.     LD    DE,QSLOTS    ;BASE OF QUEUE ENTRIES
  685.     ADD    HL,DE
  686.     POP    DE        ;HL IS NOW THE ADDRESS OF MY SLOT
  687.     LD    (HL),C
  688.     INC    HL
  689.     LD    (HL),B        ;SAVE #ARG OR ARG#1
  690.     INC    HL
  691.     LD    (HL),E
  692.     INC    HL
  693.     LD    (HL),D        ;SAVE A(RTN) OR ARG#2
  694.     RET
  695.  
  696.         ;OS_TASK()    POP ITEM OFF QUEUE AND EXECUTE
  697.         ;        WILL HALT CPU IF NOTHING WAITING
  698.         ;        AND NEVER RETURN!
  699. OS_TASK:
  700.     LD    (IXSAVE),IX    ;SAVE C REGS
  701.     LD    (IYSAVE),IY
  702.     LD    HL,0
  703.     ADD    HL,SP
  704.     LD    (SPSAVE),HL    ; This should Never return
  705.  
  706. UNTIL_CRASH:
  707.     DI    ;INTS OFF!!!!!
  708.  
  709.     LD    A,(QUEUE+QUSED)    ;HOW MANY ARE IN USE?
  710.     OR    A        ;Anything to Run?
  711.     JP    NZ,CCALL    ;Yes!! Call the C Function!
  712.  
  713.     EI    ;INTS ON!!!!!!
  714.     CALL    000Dh        ;Wait here for an interrupt (HALT!)
  715.     JP    UNTIL_CRASH    ;See what Havoc it caused...
  716.  
  717. restore:
  718.     LD    HL,(SPSAVE)    ;Restore Stack - It has args left on it
  719.     LD    SP,HL
  720.     LD    IX,(IXSAVE)
  721.     LD    IY,(IYSAVE)
  722.     JP    UNTIL_CRASH    ;Do next thing
  723.  
  724. CCALL:    CALL    GET_TOP        ;GET TOP ITEM, RET IN BC/DE
  725.     LD    (TRTN),DE    ;A(ROUTINE)
  726.     LD    A,C        ;# ARGS
  727.     CP    0        ;NONE?
  728.     JP    Z,TNOMO        ;Theres NO MOre
  729.     PUSH    AF
  730.     CALL    GET_TOP
  731.     POP    AF
  732.     CP    1
  733.     JP    Z,1f        ;ONLY 1 ARG
  734.     PUSH    DE        ;PUSH ARG #2
  735. 1:    PUSH    BC        ;PUSH ARG #1
  736.  
  737. TNOMO:    EI    ;INTS ON!!!!!!
  738.     LD    HL,(TRTN)    ;GET THE ROUTINE WE NEED TO JUMP TO
  739.     LD    A,0C0h
  740.     LD    (_COLD),A    ;Flag that we are running a process
  741.     CALL    indir        ;DO IT, GOOD LUCK
  742.     XOR    A
  743.     LD    (_COLD),A    ;All Done!
  744.  
  745. ;            Make sure we don't run out of memory!!!!
  746.     ld    hl,8        ;Insure our ports have at least 8 buffers
  747.     push    hl
  748.     ld    bc,0        ;Port number
  749.     push    bc
  750. givlop:    ld    hl,340        ;Number of bytes/buffer
  751.     push    hl
  752.     call    _new_buffer    ;Allocate it!
  753.     pop    de        ;fix stack
  754.     ld    a,l
  755.     or    h
  756.     jp    z,no_buf    ;No more buffers this big
  757.     ex    (sp),hl        ;make rval arg (swap pkt and chan)
  758.     push    hl        ;push chan
  759.     call    _RX_GIVE
  760.     pop    bc        ;Port number
  761.     pop    de        ;trash
  762.     push    bc        ;Stack has Port and Depth
  763.     ld    a,l
  764.     or    h        ;True if we want more
  765.     jp    nz,givlop
  766.     pop    bc        ;reclaim bc to modify it
  767.     inc    bc
  768.     push    bc        ;save it
  769.     ld    hl,-NUMCH    ;Do it for all ports
  770.     add    hl,bc        ;If result is =>0 we are done
  771.     jp    nc,givlop
  772.     pop    hl
  773.     pop    hl
  774.     jp    restore
  775.  
  776. no_buf:    pop    hl
  777.     pop    hl
  778.     ld    a,040H
  779.     ld    (_COLD),a    ;Note that we ran out of memory
  780.     JP    restore
  781.  
  782. GET_TOP:
  783.     LD    A,(QUEUE+QTOP)    ;GET CURRENT TOP
  784.     LD    L,A
  785.     LD    H,0
  786.     ADD    HL,HL        ;TOP*2
  787.     ADD    HL,HL        ;TOP*4
  788.     LD    DE,QSLOTS
  789.     ADD    HL,DE        ;ADDRESS OF TOP SLOT
  790.     LD    C,(HL)
  791.     INC    HL
  792.     LD    B,(HL)
  793.     INC    HL
  794.     LD    E,(HL)
  795.     INC    HL
  796.     LD    D,(HL)
  797.     LD    HL,(QUEUE)    ;H=#USED L=MAX
  798.     DEC    H
  799.     LD    (QUEUE),HL
  800.     JP    NZ,2f
  801.     LD    HL,0        ;CLEAR TOP&BOT
  802.     LD    (QUEUE+2),HL
  803.     RET
  804. 2:    LD    A,(QUEUE+QTOP)
  805.     INC    A
  806.     AND    L
  807.     LD    (QUEUE+QTOP),A
  808.     RET
  809.